# -*- coding: UTF-8 -*-
import socket
import time
data="1A20-crc-8900001E003A000102030238695807CA21790200000000030820500105D2006C0107083A081408FC0734002A00-1-D000670106083D081008FC072C002200-2-CE007D010C082E081408FC0728002400-3-CC0086010B082B081908FE0728002000-4-C9008001080836081208FD0726002200-5-"
data2="1A2000008900001e003A000102030238695807CA21790200000000030820500105D2006C0107083A081408FC0734002A00130B050F030FD000670106083D081008FC072C002200130B050F0537CE007D010C082E081408FC0728002400130B050F0822CC0086010B082B081908FE0728002000130B050F0B0CC9008001080836081208FD0726002200130B050F0D33"
# bits=bytearray.fromhex(data)
#
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#
# # 连接服务端
s.connect(('210.72.128.72', 12680))
# s.send(bits)
#
# # 关闭 socket
# s.close()
def addbite(str):
    return str if len(str)==2 else '0'+str
def crc16(x, invert):
    a = 0xFFFF
    b = 0xA001
    for byte in x:
        a ^= byte
        for i in range(8):
            last = a % 2
            a >>= 1
            if last == 1:
                a ^= b
    s = hex(a).upper()
    return s[4:6]+s[2:4] if invert == True else s[2:4]+s[4:6]
while True:
    realdata=data
    for i in range(1,6):
        timestr=time.strftime("%y%m%d%H%M%S", time.localtime())
        dd=[]
        dd.append(addbite(str(hex(int(timestr[0:2])))[2:]))
        dd.append(addbite(str(hex(int(timestr[2:4])))[2:]))
        dd.append(addbite(str(hex(int(timestr[4:6])))[2:]))
        dd.append(addbite(str(hex(int(timestr[6:8])))[2:]))
        dd.append(addbite(str(hex(int(timestr[8:10])))[2:]))
        dd.append(addbite(str(hex(int(timestr[10:12])))[2:]))
        datatime=''.join(dd)
        print(len(datatime))
        realdata=realdata.replace('-'+str(i)+'-',datatime)
        time.sleep(1)
    crc=crc16(bytearray.fromhex(realdata[13:]),True)
    if len(crc)==3:
        crc='0'+crc
    elif len(crc)==2:
        crc='00'+crc
    realdata = realdata.replace('-crc-',crc)
    print(realdata)
    print(len(realdata))
    bits=bytearray.fromhex(realdata)
    s.send(bits)
#s.close()
# print(19&0xFF)
